package com.example.examapp.service.impl;

import com.example.examapp.enums.QuestionType;
import com.example.examapp.model.Answer;
import com.example.examapp.model.Question;
import com.example.examapp.service.QuestionService;
import com.mongodb.client.result.DeleteResult;
import org.lzx.base.service.impl.BaseServiceImpl;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author liuzhengxing
 * @version v1.0
 * @package com.example.examapp.service.impl
 * @data 2022/12/4 11:02
 */
@Service
public class QuestionServiceImpl extends BaseServiceImpl<Question> implements QuestionService {
    @Override
    public List<Question> findByType(QuestionType type) {
        Assert.notNull(type,"type不能为空");
        return super.mongoTemplate.find(new Query(Criteria.where("type").is(type)),Question.class);
    }

    @Override
    public Question getById(String id) {
        Assert.hasText(id,"id不能为空");
        return super.mongoTemplate.findOne(new Query(Criteria.where("id").is(id)),Question.class);
    }

    @Override
    public DeleteResult del(String id) {
        DeleteResult del = super.del(id);
        DeleteResult questionId = super.mongoTemplate.remove(new Query(Criteria.where("questionId").is(id)), Answer.class);
        return questionId;
    }
}
